package com.iot08.admin.controller;

import com.iot08.admin.controller.base.BaseController;
import com.iot08.admin.service.QusetionTitleService;
import com.iot08.admin.service.QusetionsService;
import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.common.entity.admin.MobNotify;
import com.iot08.common.entity.admin.SysUser;
import com.iot08.common.entity.app.Questions;
import com.iot08.common.entity.app.QuestionsTitle;
import com.iot08.common.response.Response;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: Weichao
 * @Date: 2024/09/02/16:40
 * @Description:
 */
@Controller
@RequestMapping("question")
public class QuestionController extends BaseController {

    @Resource
    private QusetionsService qusetionsService;

    @Resource
    private QusetionTitleService qusetionTitleService;


    @RequestMapping("category")
    public ModelAndView categoryList(PageVO page) {
        ModelAndView mv = new ModelAndView();
        Page<Questions> pages = qusetionsService.list(page, this.getSessionUser());
        mv.addObject("categoryList", pages.getContent());
        if (pages.getPageable() instanceof PageableImpl) {
            PageableImpl pi = (PageableImpl) pages.getPageable();
            mv.addObject("page", pi.getPage());
        }
        mv.addObject("butSet", getMyBut(page.getMenuId()));
        mv.setViewName("question/category");
        return mv;
    }

    /**
     * 问题类别添加
     */
    @RequestMapping("category/toAdd")
    public ModelAndView categoryToAdd() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("question/category_add");
        return mv;
    }

    /**
     * 问题类别添加
     */
    @RequestMapping(value = "category/add", method = RequestMethod.POST)
    public ModelAndView categoryAdd(Questions category) throws IOException {
        ModelAndView mv = new ModelAndView();
        SysUser u = this.getSessionUser();
//        category.setCreator(u.getId());
        qusetionsService.add(category, this.getSessionUser(), this.getRemortIP());
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * 编辑 问题类别
     */
    @RequestMapping("category/toEdit")
    public ModelAndView categoryToEdit(String id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("category", qusetionsService.findById(id));
        mv.setViewName("question/category_edit");
        return mv;
    }

    /**
     * 编辑 问题类别
     */
    @RequestMapping(value = "category/edit", method = RequestMethod.POST)
    public ModelAndView categoryEdit(Questions category) throws IOException {
        ModelAndView mv = new ModelAndView();
        qusetionsService.update(category, this.getSessionUser(), this.getRemortIP());
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * 删除 问题类别
     */
    @ResponseBody
    @RequestMapping(value = "category/del", method = RequestMethod.POST)
    public Response<Object> categoryDel(String id) {
        boolean res = qusetionsService.updateDel(id, this.getSessionUser(), this.getRemortIP());
        if (!res) {
            return Response.failedResponse();
        }
        return Response.successResponse();
    }


    /**
     * 问题内容
     * @param page
     * @return
     */
    @RequestMapping("content")
    public ModelAndView contentList(PageVO page) {
        ModelAndView mv = new ModelAndView();
        Page<QuestionsTitle> pages = qusetionTitleService.list(page, this.getSessionUser());
        mv.addObject("contentList", pages.getContent());
        if (pages.getPageable() instanceof PageableImpl) {
            PageableImpl pi = (PageableImpl) pages.getPageable();
            mv.addObject("page", pi.getPage());
        }

        mv.addObject("categoryList", qusetionsService.onlyGetList());
        mv.addObject("butSet", getMyBut(page.getMenuId()));
        mv.setViewName("question/content");
        return mv;
    }

    /**
     * 问题内容添加
     */
    @RequestMapping("content/toAdd")
    public ModelAndView contentToAdd() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("categoryList", qusetionsService.onlyGetList());
        mv.setViewName("question/content_add");
        return mv;
    }

    /**
     * 问题内容添加
     */
    @RequestMapping(value = "content/add", method = RequestMethod.POST)
    public ModelAndView contentAdd(QuestionsTitle content) throws IOException {
        ModelAndView mv = new ModelAndView();
        SysUser u = this.getSessionUser();
//        content.setCreator(u.getId());
        Questions category = qusetionsService.findById(content.getCategoryid());
        content.setCategory(category.getCategory());
        qusetionTitleService.add(content, this.getSessionUser(), this.getRemortIP());
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * 编辑 问题内容
     */
    @RequestMapping("content/toEdit")
    public ModelAndView contentToEdit(String id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("content", qusetionTitleService.findById(id));
        mv.addObject("categoryList", qusetionsService.onlyGetList());
        mv.setViewName("question/content_edit");
        return mv;
    }

    /**
     * 编辑 问题内容
     */
    @RequestMapping(value = "content/edit", method = RequestMethod.POST)
    public ModelAndView contentEdit(QuestionsTitle content) throws IOException {
        ModelAndView mv = new ModelAndView();
        Questions category = qusetionsService.findById(content.getCategoryid());
        content.setCategory(category.getCategory());
        qusetionTitleService.update(content, this.getSessionUser(), this.getRemortIP());
        mv.addObject("msg", "success");
        mv.setViewName("save_result");
        return mv;
    }

    /**
     * 删除 问题内容
     */
    @ResponseBody
    @RequestMapping(value = "content/del", method = RequestMethod.POST)
    public Response<Object> contentDel(String id) {
        boolean res = qusetionTitleService.updateDel(id, this.getSessionUser(), this.getRemortIP());
        if (!res) {
            return Response.failedResponse();
        }
        return Response.successResponse();
    }

    /**
     * 变更 问题内容 状态
     */
    @ResponseBody
    @RequestMapping(value = "content/change", method = RequestMethod.POST)
    public Response<Object> contentChange(String id) {
        boolean res = qusetionTitleService.change(id, this.getSessionUser(), this.getRemortIP());
        if (!res) {
            return Response.failedResponse();
        }
        return Response.successResponse();
    }



}
